/*
工厂方法模式Factory Method,又称多态性工厂模式。
在工厂方法模式中,核心的工厂类不再负责所有的产品的创建，而是将具体创建的工作交给工厂子类去做。
*/
export {}

abstract class Coffee {
  constructor(public name: string){

  }
}

class AmericanCoffee extends Coffee{
}

class LatteCoffee extends Coffee{
}

class CappuccinoCoffee extends Coffee{
}

abstract class CateFactory {
  abstract createCoffee(): Coffee;
}

class AmericanCoffeeFactory extends CateFactory{
  createCoffee(): Coffee {
    return new AmericanCoffee('美式咖啡')
  }
}

class LatteCoffeeFactory extends CateFactory{
  createCoffee(): Coffee {
    return new AmericanCoffee('拿铁咖啡')
  }
}

class CappuccinoCoffeeFactory extends CateFactory{
  createCoffee(): Coffee {
    return new AmericanCoffee('卡布奇诺咖啡')
  }
}

// const americanCoffeeFactory = new AmericanCoffeeFactory()
// console.log(americanCoffeeFactory.createCoffee())
// const latteCoffeeFactory = new LatteCoffeeFactory()
// console.log(latteCoffeeFactory.createCoffee())
// const cappuccinoCoffeeFactory = new CappuccinoCoffeeFactory()
// console.log(cappuccinoCoffeeFactory.createCoffee())


//↓核心的工厂类
class CafeFactory {
  static order(name: string){
    switch (name) {
    case 'AmericanCoffee':
      return new AmericanCoffeeFactory()/*←工厂子类*/.createCoffee()
    case 'LatteCoffee':
      return new LatteCoffeeFactory().createCoffee()
    case 'CappuccinoCoffee':
      return new CappuccinoCoffeeFactory().createCoffee()
    default:
      throw new Error('你点的咖啡品种我们不支持')
    }
  }
}
